<template>
  <div class="login-container">
    <el-form class="login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
<!--      <h3 class="title">{{title}}</h3>-->
      <img src="./pic.png" class="img">
      <div class="msg">
          <div class="titleName">抖兴</div>
          <div class="name">店铺管理后台</div>
          <div class="detail">健康产品一站式购物平台</div>
          <el-form-item prop="username">
            <span class="svg-container svg-container_login">
                <img src="./person.png" alt="">
            </span>
              <el-input name="username" type="text" v-model="loginForm.username"  placeholder="账号/手机号" />
          </el-form-item>
          <el-form-item prop="password">
        <span class="svg-container">
          <img src="./knock.png" alt="">
        </span>
              <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password"
                        placeholder="密码"></el-input>
              <span class="show-pwd" @click="showPwd">
                  <img src="./eyeO.png" alt="" v-if="eye">
                  <img src="./eyeC.png" alt="" v-if="!eye">
              </span>
          </el-form-item>

          <el-form-item  prop="kaptcha">
              <!--<span class="svg-container svg-container_login">-->
              <!--<svg-icon icon-class="user" />-->
              <!--</span>-->

              <el-input style="float: left;width: 60%"   name="kaptcha" type="text" v-model="loginForm.captcha"  placeholder="请输入验证码" />
              <span class="show-pwd" ><svg-icon icon-class="example" /></span>
              <img @click="updateKaptcha" style="width: 100px;height: 40px;float: right" :src="kaptcha" alt="">
          </el-form-item>

          <el-form-item style="border: none;" >
              <el-button type="primary" style="width:320px;border-radius: 33px;height: 45px" :loading="loading" @click.native.prevent="handleLogin"  @keyup.enter="handleAddBook">
                  登录
              </el-button>
          </el-form-item>
      </div>
<!--      <div class="tips">-->
<!--        <span style="margin-right:20px;">username: admin</span>-->
<!--        <span> password: admin</span>-->
<!--      </div>-->
    </el-form>
  </div>
</template>

<script>
// import { isvalidUsername } from '@/utils/validate'
import { kaptcha } from '../../api/login'
import {Message} from 'element-ui'
import defaultSetting from '@/settings'
export default {
  name: 'login',
  created() {
    this.updateKaptcha()
    this.keyupEnter()
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      callback()
      // if (!isvalidUsername(value)) {
      //   callback(new Error('请输入正确的用户名'))
      // } else {
      //   callback()
      // }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    return {
      kaptcha: '',
      loginForm: {
        username: null,
        password: null,
        captcha: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }],
        captcha: [{ required: true, trigger: 'blur' }]
      },
      loading: false,
      pwdType: 'password',
        eye:true
    }
  },
  computed:{
    title(){
      return defaultSetting.title;
    }
  },
  methods: {
    keyupEnter(){
      document.onkeydown = e =>{
        let _key=window.event.keyCode;
        if(_key===13){
          this.handleLogin()
        }
      }
    },
    updateKaptcha() {
      this.kaptcha = kaptcha() + '?date=' + new Date()
    },
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
      this.eye = !this.eye
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: '/' })
          }).catch(res => {
            this.updateKaptcha()
            this.loginForm.captcha = ''
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;

/* reset element-ui css */
.login-container {
  background:url('./bg.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 80%;
    &.captcha{
      width: 35%;
    }
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    /*background: rgba(0, 0, 0, 0.1);*/
    border-radius: 5px;
    color: #454545;
    border-bottom:1px solid #eee ;
  }
}

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
  /*position: fixed;*/

  height: 100%;
  width: 100%;
  background-color: $bg;
    display: flex;
    justify-content: center;
    align-items: center;
  .login-form {
    background:url("./box.png");
    background-size: 100% 100%;
    background-repeat: no-repeat;
    width: 1100px;
    height: 750px;
    /*padding: 35px 35px 15px 35px;*/
    /*padding: 0 9.2%;*/
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;
      .img{
          width: 550px;
          height: 376px;
      }
      .msg{
          width: 360px;
          height: 100%;
          padding-top: 147px;
          margin-left: 87px;
          .titleName{
              color: #5094fe;
              font-size: 30px;
          }
          .name{
              color: #343434;
              font-size: 44px;
          }
          .detail{
              margin-top: 12px;
              color: #aaaaaa;
              font-size: 18px;
              margin-bottom: 62px;
          }
      }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    img{
        width: 18px;
        height: 18px;
    }
    &_login {
      font-size: 20px;
    }
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
      img{
          width: 40px;
          height: 40px;
      }
  }
}
</style>
